package com.csr.csrcheck.controller;

import com.csr.csrcheck.pojo.Company;
import com.csr.csrcheck.pojo.Five_supplier;
import com.csr.csrcheck.service.CompanyService;
import com.csr.csrcheck.service.Five_SupplierService;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.util.JsonResult;
import com.csr.csrcheck.util.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping({"Five"})
public class Five_SupplierController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(com.csr.csrcheck.controller.Five_SupplierController.class);

    @Resource
    private Five_SupplierService fiveSupplierService;

    @Resource
    private CompanyService companyService;

    @GetMapping({"fiveSuppler"})
    public JsonResult<PageResult> fiveSuppler(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, String supplier_name) {
        PageResult pageResult = null;
        try {
            pageResult = this.fiveSupplierService.getListFive(pageNum, pageSize, supplier_name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("fiveSuppler------------------>pageResult" + pageResult);
        log.info("fiveSuppler------------------>pageNum" + pageNum);
        log.info("fiveSuppler------------------>pageSize" + pageSize);
        log.info("fiveSuppler------------------>supplier_name" + supplier_name);
        return new JsonResult(Integer.valueOf(0), "json", pageResult);
    }

    @RequestMapping(path = {"addFive"}, method = {RequestMethod.POST})
    public JsonResult<Void> addFive(Five_supplier five_Supplier) {
        this.fiveSupplierService.addFivesupplier(five_Supplier);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"updateFive"}, method = {RequestMethod.POST})
    public JsonResult<Void> updateFive(Five_supplier five_Supplier) {
        this.fiveSupplierService.updateFiveSupplier(five_Supplier);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"deleteFive/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Void> deleteFive(@PathVariable("id") int id) {
        this.fiveSupplierService.deleteFiveSupplier(id);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"findFive/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Five_supplier> findFive(@PathVariable("id") int id) {
        Five_supplier five_Supplier = this.fiveSupplierService.fingfiveByid(id);
        if (five_Supplier == null)
            throw new CompanyException("");
        return new JsonResult(Integer.valueOf(0), "json", five_Supplier);
    }

    @RequestMapping(path = {"showcompany"}, method = {RequestMethod.POST})
    public JsonResult<List<Company>> showcompany() {
        List<Company> list = this.companyService.getCommpanylist();
        return new JsonResult(Integer.valueOf(0), "json", list);
    }
}
